var express = require('express');
var router = express.Router();
var User = require('../db/user.js');
var article = require('../db/article');


//评论
router.post("/doCommit", function (req, res, next) {
    let aid = req.body.aid;
    let comment = req.body.comment;
    let addTime = req.body.addTime;
    User.findOne({ uid: req.cookies.userID }, function (err, data) {
        if (err) { console.log(err); return; }
        article.updateOne({ a_id: aid }, { $addToSet: { comment: { c_id: "arCo/" + req.cookies.userID + "/" + addTime, c_name: data.uname, c_imgHead: data.img_head, c_addTime: addTime, c_content: comment, c_type: false, } } }, function (err) {
            if (err) { console.log(err); return; }
            else {
                res.send("评论成功！")
            }
        });
    })
});

//收藏
router.post("/doCollection", function (req, res, next) {
    article.findOne({ a_id: req.body.aid }, function (err, list) {
        if (err) { console.log(err); return; }
        if (list.collectionCount.length != 0) {
            for (let i = 0; i < list.collectionCount.length; i++) {
                if (req.cookies.userID == list.collectionCount[i]) {
                    res.send("你已经收藏过了哦！");
                    return;
                } else { }
            }
        }
        article.updateOne({ a_id: req.body.aid }, { $addToSet: { collectionCount: req.cookies.userID } }, function (err) {
            if (err) {
                console.log(err);
                return;
            } else {
                User.updateOne({ uid: req.cookies.userID }, { $addToSet: { likes: req.body.aid } }, function (err) {
                    if (err) { console.log(err); return; }
                    res.send("收藏成功！");
                })
            }
        });
    })
});

//home主页文章列表获取
router.post("/doHome", function (req, res, next) {
    article.find({ a_type: 1 }, function (err, data) {
        if (err) { console.log(err); return; }
        if (data.length != 0 && data.length < 30) {
            let allArticle = [];
            for (let i = data.length - 1; i >= 0; i--) {
                allArticle.push({
                    aid: data[i].a_id,
                    uname: data[i].uname,
                    title: data[i].title,
                    intr: data[i].intr,
                    coverImg: data[i].coverImg,
                    viewNum: data[i].viewCount,
                    likeNum: data[i].likeCount.length,
                    collectionNum: data[i].collectionCount.length,
                    commentNum: data[i].comment.length,
                });
            }
            res.send(allArticle);
        } else if (data.length >= 30) {
            let allArticle = [];
            for (let i = data.length - 1; i >= data.length - 30; i--) {
                allArticle.push({
                    aid: data[i].a_id,
                    uname: data[i].uname,
                    title: data[i].title,
                    intr: data[i].intr,
                    coverImg: data[i].coverImg,
                    viewNum: data[i].viewCount,
                    likeNum: data[i].likeCount.length,
                    collectionNum: data[i].collectionCount.length,
                    commentNum: data[i].comment.length,
                })
            }
            res.send(allArticle);
        } else {
            res.send("");
        }
    })
});

//点赞
router.post("/doLike", function (req, res, next) {
    article.findOne({ a_id: req.body.aid }, function (err, list) {
        if (err) { console.log(err); return; }
        if (list.likeCount.length != 0) {
            for (let i = 0; i < list.likeCount.length; i++) {
                if (req.cookies.userID == list.likeCount[i]) {
                    res.send("你已经点过赞了哦！");
                    return;
                } else { }
            }
        }
        article.updateOne({ a_id: req.body.aid }, { $addToSet: { likeCount: req.cookies.userID } }, function (err) {
            if (err) {
                console.log(err);
                return;
            } else {
                User.updateOne({ uid: list.uid }, { $inc: { numOfLikes: 1 } }, function (err) {
                    if (err) { console.log(err); return; }
                    res.send("点赞成功！");
                })
            }
        });
    })
});

module.exports = router;